﻿//==============================================================================
// File: Shell.xaml.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Windows;
using WpfRcp.Controls;
using WpfRcp.Core.Views;
using WpfRcp.Core.Views.Presenters;
using WpfRcp.Helpers;
using WpfRcp.Core.Globals;

namespace WpfRcp.Views.Windows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Shell : IShell
    {
        // Consts
        public static readonly GridLength SplitterSize = new GridLength(3.0);
        public const double VisualizationMenuMinWidth = 200.0;
        public const double VisualizationMenuMinHeight = 200.0;
        public const double RightMenuMinWidth = 250.0;
        public static readonly GridLength RightMenuDefaultWidth = new GridLength(364.0);
        public const double BottomMenuMinHeight = 25.0;
        public static readonly GridLength BottomMenuDefaultHeight = new GridLength(200.0);

        // Fields
        private GridLength m_PreviousRightMenuWidth = RightMenuDefaultWidth;
        private GridLength m_PreviousBottomMenuHeight = BottomMenuDefaultHeight;

        public Shell(IMenuControl control)
        {
            InitializeComponent();

            if (Environment.OSVersion.Version.Major < 6 || GlassHelper.IsGlassEnabled == false)
            {
                ChromeWindowStyle = ChromeWindowStyle.Normal;
                ResizeMode = ResizeMode.NoResize;
                WindowStyle = WindowStyle.None;
                AllowsTransparency = true;
                BorderThickness = new Thickness(1);
            }

            m_HeaderPanel.Children.Add(control as UIElement);
        }

        private void OnRightTabMenuVisibilityChanged(object sender, VisibilityChangedRoutedEventArgs e)
        {
            if (e.Visibility == Visibility.Visible)
            {
                m_SplitterColumn.Width = SplitterSize;
                m_RightTabMenuColumn.Width = m_PreviousRightMenuWidth;
                m_RightTabMenuColumn.MinWidth = RightMenuMinWidth;
                m_GridSplitter.Visibility = Visibility.Visible;
            }
            else
            {
                m_PreviousRightMenuWidth = m_RightTabMenuColumn.Width;
                m_SplitterColumn.Width = new GridLength(0.0);
                m_RightTabMenuColumn.Width = GridLength.Auto;
                m_RightTabMenuColumn.MinWidth = 0.0;
                m_GridSplitter.Visibility = Visibility.Collapsed;
            }
        }

        private void OnBottomTabMenuVisibilityChanged(object sender, VisibilityChangedRoutedEventArgs e)
        {
            if (e.Visibility == Visibility.Visible)
            {
                m_SplitterRow.Height = SplitterSize;
                m_BottomTabMenuRow.Height = m_PreviousBottomMenuHeight;
                m_BottomTabMenuRow.MinHeight = BottomMenuMinHeight;
                m_VerticalGridSplitter.Visibility = Visibility.Visible;
            }
            else
            {
                m_PreviousBottomMenuHeight = m_BottomTabMenuRow.Height;
                m_SplitterRow.Height = new GridLength(0.0);
                m_BottomTabMenuRow.Height = GridLength.Auto;
                m_BottomTabMenuRow.MinHeight = 0.0;
                m_VerticalGridSplitter.Visibility = Visibility.Collapsed;
            }
        }

        #region IShell Members

        public IShellPresentationModel PresentationModel
        {
            set { DataContext = value; }
        }

        public object ViewOwner
        {
            set { throw new ApplicationException("Cannot assign owner to the shell view."); }
        }

        public ITabMenuItem GetTabMenuItemInstance(string regionName)
        {
            switch (regionName)
            {
                case RegionNames.DocumentRegion:
                    return new TabMenuItem
                    {
                        DetachedMargin = new Thickness(0.0),
                        IsCloseable = true,
                        IsDetachable = true
                    };
                case RegionNames.BottomRegion:
                case RegionNames.SidePanelRegion:
                    return new TabMenuItem
                    {
                        IsDetachable = true,
                        DetachedMargin = new Thickness(3.0)
                    };
                default:
                    return null;
            }
        }
        #endregion
    }
}